/**
 * Copyright (c) 2020-2024, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.leetcode;

/**
 * https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/
 *
 * @author Xue Xue (xuexue1@huli.com)
 * @version 1.0
 * @since 2020/8/26
 */
public class Offer68_1 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            while (root != null) {
                if (p.val > root.val && q.val > root.val) {
                    root = root.right;
                } else if (p.val < root.val && q.val < root.val) {
                    root = root.left;
                }
                break;
            }
            return root;
        }
    }
}
